<?php
/**
 * 后台 企业风格 控制器
 *
 */
class StyleAction extends Action
{
    /**
     * 管理所有风格
     */
    public function AdminIndex()
    {
    	EasyACL('Style','AdminIndex',$_SESSION);
    	$Style = D('Style');
    	$count = $Style->count(NULL,'sid');
    	$listRows = 15;
    	$fields = 'sid,name,style_path,vip1,vip2,vip3';
    	import('ORG.Util.Page');
    	$p = new Page($count,$listRows);
    	$list = $Style->findall(NULL,$fields,'sid desc',$p->firstRow .','.$p->listRows);
    	$page = $p->show();
    	
    	//To Templates
    	$this->assign('Styles',$list);
    	$this->assign('page',$page);
    	$this->display();
    }
    
    /**
     * 创建风格
     */
    public function AdminCreate()
    {
    	EasyACL('Style','AdminCreate',$_SESSION);
    	//POST
    	if ($this->isPost()) {
    		if ($_FILES['file']['name'] != '') {
				//上传文件
				import("ORG.Net.UploadFile");
				$upload = new UploadFile();
				//设置上传文件大小
				$upload->maxSize  = 266240;
				//设置上传文件类型
				$upload->allowExts  = explode(',','jpg,gif,png');
				//设置附件上传目录
				$upload->savePath =  './upload/'; 
			
				//执行上传操作
				if(!$upload->upload()) {
					$this->assign("errMsg",$upload->getError());   
				}else{ 
					$AllFiles = $upload->getSuccessFiles();
				}
    		}
			$Input = Input::getInstance();
			$Input->filter('addslashes');
			
			$Q = '';
			foreach ($_POST['q'] as $a) {
				$Q .= $a.',';
			}
			//数据库操作
			$Style = D('Style');
			$Style->name = $Input->post('name');
			$Style->style_path = $Input->post('style_path');
			$Style->simg = './upload/'.$AllFiles[0]['savename'];
			$Style->vip1 = $_POST['vip1'];
			$Style->vip2 = $_POST['vip2'];
			$Style->vip3 = $_POST['vip3'];
			$time = time();
			$Style->created = $time;
			$Style->updated = $time;
			
			if ($Style->add()) {
				return $this->redirect('AdminIndex','Style');
			} else {
				return $this->error($Style->getError());
			}
    	}
    	//Templates
    	$this->display();
    }
    
    /**
     * 修改风格
     */
    public function AdminUpdate()
    {
    	EasyACL('Style','AdminUpdate',$_SESSION);
    	$Input = Input::getInstance();
    	$Input->filter('addslashes');
    	
    	$Style = D('Style');
    	
    	import('ORG.Util.HashMap');
    	$conditions = new HashMap();
    	
    	if ($this->isPost()) {
    		if ($_FILES['file']['name'] != '') {
    			//上传文件
				import("ORG.Net.UploadFile");
				$upload = new UploadFile();
				//设置上传文件大小
				$upload->maxSize  = 266240;
				//设置上传文件类型
				$upload->allowExts  = explode(',','jpg,gif,png');
				//设置附件上传目录
				$upload->savePath =  './upload/'; 
			
				//执行上传操作
				if(!$upload->upload()) {
					$this->assign("errMsg",$upload->getError());   
				}else{ 
					$AllFiles = $upload->getSuccessFiles();
				}
    		}
    		$conditions->put('sid',array('EQ',$Input->post('sid')));
    		$Style->where($conditions)->find();
    		$Style->name = $Input->post('name');
    		$Style->style_path = $Input->post('style_path');
    		if (isset($AllFiles)) {
    			$Style->simg = './upload/'.$AllFiles[0]['savename'];
    		}
			$Style->vip1 = $_POST['vip1'];
			$Style->vip2 = $_POST['vip2'];
			$Style->vip3 = $_POST['vip3'];
			$Style->updated = time();
			if ($Style->save()) {
				return $this->success('修改成功!');
			} else {
				return $this->error('尝试修改风格失败');
			}
    	}
    	$conditions->put('sid',array('EQ',$Input->get('sid')));
    	//获取要修改的风格信息
    	$S = $Style->where($conditions)->find();
    	
    	//To templates
    	$this->assign('Style',$S);
    	$this->display();
    }
    
    /**
     * 删除风格
     */
    public function AdminDel()
    {
    	EasyACL('Style','AdminDel',$_SESSION);
    	$Input = Input::getInstance();
    	$Input->filter('addslashes');
    	
    	import('ORG.Util.HashMap');
    	$conditions = new HashMap();
    	$conditions->put('sid',array('EQ',$Input->get('sid')));
    	
    	$Style = D('Style');
    	$S = $Style->where($conditions)->find();
    	unlink($S['simg']);
    	if ($Style->deleteById($Input->get('sid'))) {
    		return $this->success('删除成功!');
    	} else {
    		return $this->error('删除失败');
    	}
    }
}
?>